//引入模块
import Api from './api.js'
import Cookie from './cookie.js'

new Vue({
    el:"#app",
    data() {
        return {
            pwd:'',

            opwd:'',
            npwd:'',
            rpwd:'',

            flag1:0,
            flag2:0,

            nc:'',
            tx:'',
            zy:'',
            info:'',
            city:'',


            //关注的账号
            carezh:[],
            //账号的粉丝
            fanszh:[]
        };
    },
    mounted(){
        this.sinfo()
    },
    methods:{
        async sinfo(){
            //获取所有用户
            let allinfo = await Api.login()
            //获取当前用户id
            let zh = sessionStorage.getItem("zh")
            //判断是否登录
            if(zh==null){
                this.$alert('请先登录！', '温馨提示', {
                    confirmButtonText: '确定',
                    callback: action => {
                        this.$message({
                            type: 'info',
                            message: `action: ${ action }`
                        });
                        window.location.href="./login.html"
                    }
                });
            }

            let info = await Api.login()
                for(let i=0;i<info.length;i++){
                    if(zh==info[i].zh){
                        this.pwd=info[i].pwd
                        this.nc = info[i].nc
                        this.tx = info[i].tx
                        this.zy = info[i].zy
                        this.info = info[i].info
                        this.city = info[i].city
                        break;
                    }
                }

            //调用关注表的内容
            let careinfo = await Api.careInfo()
            let caremsg = careinfo.data
            //获取
            for (let i=0;i<caremsg.length;i++){
                for (let j=0;j<allinfo.length;j++){
                    //关注的账号
                    if(caremsg[i].user_zh==zh){
                        if(caremsg[i].care_zh==allinfo[j].zh){
                            this.carezh.push(allinfo[j])
                        }
                    }
                    //粉丝的账号
                    if(caremsg[i].care_zh==zh){
                        if(caremsg[i].user_zh==allinfo[j].zh){
                            this.fanszh.push(allinfo[j])
                        }
                    }
                }
            }

        },

        //原始密码验证
        p1(){
            if(this.opwd==this.pwd){
                this.flag1=2
            }else {
                this.flag1=1
            }
        },
        //新密码验证
        p2(){
            let regExp = /^[A-Za-z0-9]{6,14}$/;
            if(!regExp.test(this.npwd)) {
                this.$message({
                    showClose: true,
                    message: '您的密码为空或输入不规范！',
                    type: 'error'
                });
            }else {
                if(this.npwd==this.rpwd){
                    this.flag2=2
                }else {
                    this.flag2=1
                }
            }
        },
        //保存
        save(){
            if(this.flag1==2&&this.flag2==2){


                let zh = sessionStorage.getItem("zh")

                Api.upPwd(zh,this.npwd)

                this.$message({
                    showClose: true,
                    message: '修改成功！',
                    type: 'success'
                });
                window.location.href='./pwd.html'
            }else {
                this.$message({
                    showClose: true,
                    message: '信息有误！',
                    type: 'error'
                });
            }
        },

        toMessage(info){
            sessionStorage.setItem("ToMessage",JSON.stringify(info))
            window.location.href="My_Messages.html"
        }
    },

})